<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户2</title>
</head>
<body>
<h2>用户2</h2>
<input type="text" id="message" placeholder="输入消息...">
<button id="sendBtn">发送</button>
<div id="messages"></div>

<script>
    const socket = new WebSocket("ws://127.0.0.1:8088/singleChat");

    socket.onopen = function() {
        console.log("用户2已连接");
    };

    socket.onmessage = function(event) {
        const messagesDiv = document.getElementById("messages");
        messagesDiv.innerHTML += `<p>用户1: ${event.data}</p>`;
    };

    document.getElementById("sendBtn").onclick = function() {
        const messageInput = document.getElementById("message");
        const message = messageInput.value;
        socket.send(message);
        messageInput.value = "";  // 清空输入框
    };
</script>
</body>
</html>
